#pragma once

#include "Screen.h"
#include "Keys.h"
#include "rm67162.h"

// 前置声明
class Battery;

// 包含图标：一级菜单
#include "icons/1nrf.h"
#include "icons/2game.h"
#include "icons/3vs.h"
#include "icons/4info.h"
#include "icons/5ble.h"
#include "icons/6set.h"

// 包含图标：二级菜单
#include "icons/1nrf/1pickup.h"
#include "icons/1nrf/2truck.h"
#include "icons/1nrf/3tank.h"
#include "icons/1nrf/4drone.h"
#include "icons/1nrf/5excavator.h"
#include "icons/1nrf/6ship.h"
#include "icons/2game/2_1snake.h"
#include "icons/2game/2_2brick.h"
#include "icons/2game/2_3plane.h"
#include "icons/2game/2_4num2048.h"
#include "icons/2game/2_5tetris.h"
#include "icons/3vs/3_1_ball.h"
#include "icons/4info/4_1_bilibili.h"
#include "icons/4info/4_2_weather.h"
#include "icons/4info/4_3_stock.h"
#include "icons/6set/6_1_keysTest.h"
#include "icons/6set/6_2_cube.h"

using Callback = void (*)(void);

// ====================================================================================
// 基础菜单类 - 提供通用菜单功能
// ====================================================================================
class BaseMenu {
protected:
  Screen& _screen;
  Keys& _keys;

  // 通用绘制方法
  void drawMenuTitle(const String& title);
  void clearMenuArea();
  void pushToScreen();

  // 通用指示器绘制（使用模板方法模式）
  void drawRoundRectIndicators(int activeIndex, int totalCount, int startY, const int positions[]);
  void drawCircleIndicators(int activeIndex, int totalCount, const int positions[]);

  // 通用图标绘制
  void drawImage(int x, int y, int width, int height, const unsigned short* image);

public:
  BaseMenu(Screen& screen, Keys& keys);
  virtual ~BaseMenu() = default;
};

// 工具函数声明（在ControllerSystem.ino中实现）
extern void drawBatteryStatus();

// ====================================================================================
// 主菜单类
// ====================================================================================
class Menu : public BaseMenu {
public:
  Menu(Screen& screen, Keys& keys);

  void show();

  // 设置回调函数
  void setNRFControlCallback(void (*callback)());
  void setLocalGameCallback(void (*callback)());
  void setVSGameCallback(void (*callback)());
  void setNetInfoCallback(void (*callback)());
  void setBtGamepadCallback(void (*callback)());
  void setSystemSetCallback(void (*callback)());

private:
  static const int MENU_COUNT = 6;
  String _menuName[MENU_COUNT];

  Callback _callbacks[MENU_COUNT] = { nullptr };

  void drawMainMenu(int menuIndex);
};

// ====================================================================================
// NRF遥控菜单类
// ====================================================================================
class NRFMenu : public BaseMenu {
public:
  NRFMenu(Screen& screen, Keys& keys);

  void show();

  void setPickupCallback(void (*callback)());
  void setTruckCallback(void (*callback)());
  void setTankCallback(void (*callback)());
  void setDroneCallback(void (*callback)());
  void setExcavatorCallback(void (*callback)());
  void setShipCallback(void (*callback)());

private:
  static const int NRF_COUNT = 6;
  String _nrfName[NRF_COUNT];

  Callback _callbacks[NRF_COUNT] = { nullptr };

  void drawNRFMenu(int nrfIndex);
};

// ====================================================================================
// 游戏菜单类
// ====================================================================================
class GameMenu : public BaseMenu {
public:
  GameMenu(Screen& screen, Keys& keys);

  void show();

  void setSnakeCallback(void (*callback)());
  void setBrickCallback(void (*callback)());
  void setPlaneCallback(void (*callback)());
  void setNum2048Callback(void (*callback)());
  void setTetrisCallback(void (*callback)());

private:
  static const int GAME_COUNT = 5;
  String _gameName[GAME_COUNT];

  Callback _callbacks[GAME_COUNT] = { nullptr };

  void drawGameMenu(int gameIndex);
};

// ====================================================================================
// 双人对战菜单类
// ====================================================================================
class VSMenu : public BaseMenu {
public:
  VSMenu(Screen& screen, Keys& keys);

  void show();

  void setBallCallback(void (*callback)());

private:
  static const int VS_COUNT = 1;
  String _vsName[VS_COUNT];

  Callback _callbacks[VS_COUNT] = { nullptr };

  void drawVSMenu(int vsIndex);
};

// ====================================================================================
// 网络信息菜单类
// ====================================================================================
class InfoMenu : public BaseMenu {
public:
  InfoMenu(Screen& screen, Keys& keys);

  void show();

  void setBilibiliCallback(void (*callback)());
  void setWeatherCallback(void (*callback)());
  void setStockCallback(void (*callback)());

private:
  static const int INFO_COUNT = 3;
  String _infoName[INFO_COUNT];

  Callback _callbacks[INFO_COUNT] = { nullptr };

  void drawInfoMenu(int infoIndex);
};

// ====================================================================================
// 系统设置菜单类
// ====================================================================================
class SystemMenu : public BaseMenu {
public:
  SystemMenu(Screen& screen, Keys& keys);

  void show();

  void setKeysTestCallback(void (*callback)());
  void setCubeCallback(void (*callback)());

private:
  static const int SYSTEM_COUNT = 2;
  String _setName[SYSTEM_COUNT];

  Callback _callbacks[SYSTEM_COUNT] = { nullptr };

  void drawSystemMenu(int setIndex);
};
